package com.zttek.photosShareProject.modules.service.share.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zttek.photosShareProject.modules.mapper.RankAllMapper;
import com.zttek.photosShareProject.modules.mapper.RankDayMapper;
import com.zttek.photosShareProject.modules.mapper.RankMonthMapper;
import com.zttek.photosShareProject.modules.mapper.RankWeekMapper;
import com.zttek.photosShareProject.modules.model.*;
import com.zttek.photosShareProject.modules.service.share.IRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Y
 * Date: 2017-09-06
 * Time: 23:30
 */
@Service
public class RankService implements IRankService {

    @Autowired
    private RankDayMapper dayMapper;

    @Autowired
    private RankWeekMapper weekMapper;

    @Autowired
    private RankMonthMapper monthMapper;

    @Autowired
    private RankAllMapper allMapper;

    @Override
    public RankDay selectRankDay(Integer dayNum) throws Exception {
        RankDay rankDay = dayMapper.selectById(dayNum);
        return rankDay;
    }

    @Override
    public RankWeek selectRankWeek(Integer weekNum) throws Exception {
        RankWeek rankWeek = weekMapper.selectById(weekNum);
        return rankWeek;
    }

    @Override
    public RankMonth selectRankMonth(Integer monthNum) throws Exception {
        RankMonth rankMonth = monthMapper.selectById(monthNum);
        return rankMonth;
    }

    @Override
    public RankAll selectRankAll() throws Exception {
        RankAll rankAll = allMapper.selectList(new EntityWrapper<>()).get(0);
        return rankAll;
    }

    @Override
    public Integer getDayNum(){
        LocalDate today = LocalDate.now();
        Long l = ChronoUnit.DAYS.between(IRankService.ZERO_DAY, today);
        return l.intValue();
    }

    @Override
    public Integer getWeekNum(){
        LocalDate today = LocalDate.now();
        Long l = ChronoUnit.WEEKS.between(IRankService.ZERO_DAY, today);
        return l.intValue();
    }

    @Override
    public Integer getMonthNum(){
        LocalDate today = LocalDate.now();
        Long l = ChronoUnit.MONTHS.between(IRankService.ZERO_DAY, today);
        return l.intValue();
    }

    @Override
    public List<Long> Rank2List(Rank rank){
        List<Long> longs = new ArrayList<>();
        if (rank == null){
            return longs;
        }
        longs.add(rank.getFirstId());
        longs.add(rank.getSecondId());
        longs.add(rank.getThirdId());
        longs.add(rank.getFourthId());
        longs.add(rank.getFifthId());
        longs.add(rank.getSixthId());
        longs.add(rank.getSeventhId());
        longs.add(rank.getEighthId());
        longs.add(rank.getNinthId());
        longs.add(rank.getTenthId());
        while (longs.contains(null))
            longs.remove(null);
        return longs;
    }

}
